home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 235 / Issue 235 - September 2007 - DPCS0907DVD.ISO / Extras / NetObjects Fusion / NOF10.exe / data1.cab / FSI / lib / nof / text / MessageFormat.js < prev    next >
Encoding:
Text File  |  2007-04-11  |  3.0 KB  |  98 lines

  1. /****i* SOURCE_FILE/INFO
  2.   *
  3.   * NAME
  4.   *  MessageFormat.js
  5.   *
  6.   * USAGE
  7.   *  Part of Netobjects JavaScript Library.
  8.   *
  9.   * COPYRIGHT
  10.   *  Copyright ⌐ 2000-2005 Website Pros, Inc.
  11.   *  All Rights Reserved.
  12.   *
  13.   *  This is an unpublished work protected by Website Pros, Inc.
  14.   *  as a trade secret, and is not to be used or disclosed except as
  15.   *  expressly provided in a written license agreement executed by
  16.   *  you and Website Pros, Inc.
  17.   *
  18.   *      <copyright@websitepros.com>
  19.   *
  20.   * NOTES
  21.   *  JavaScript code.
  22.   *
  23.   *****/
  24. if (!IS.isModuleInitialized("IS.NOF.TEXT.MessageFormat"))
  25.   
  26.   /****h* NOF_JavaScript_Library/NOF.TEXT.MessageFormat
  27.     *
  28.     * NAME
  29.     *  NOF.TEXT.MessageFormat
  30.     *
  31.     * DESCRIPTION
  32.     *
  33.     * <code>MessageFormat</code> is a class which provides a simple way to 
  34.     * format a text using parameters.
  35.     * Usage sample:
  36.     * var myPattern = "{0} is smaller than {1}, thus {1} is greater than {0}";
  37.     *   var myParams = ["the number 3", "the number 4"];
  38.     *   var str = NOF.TEXT.MessageFormat.format(myPattern, myParams);
  39.     *   alert( str );
  40.     *
  41.     * External dependencies: none.
  42.     ****/
  43.   
  44.   /**
  45.     * Constructor
  46.     **/
  47.   function TEXT_MessageFormat() {
  48.     this.__proto__ = TEXT_MessageFormat.prototype;
  49.   }
  50.   {
  51.     var method = TEXT_MessageFormat.prototype;                
  52.     
  53.     /**
  54.     * static method used to format a message
  55.     * @param pattern the pattern for this message format
  56.     * @param params an array of objects (strings) to be formatted and substituted.
  57.     * @return the string obtained by replacing {x} sequences from the pattern with
  58.     * params[x] values, where x are indices from 0 to params length
  59.     **/
  60.     method.format = function (/*string*/ pattern, /*Array*/ params) {
  61.       if (pattern == null) return null;
  62.       if (params == null) return pattern;
  63.       
  64.       var parameters;
  65.       
  66.       //if (params.constructor == Array) {                
  67.       //the previous line should have been working, but it doesn't! 
  68.       //the next lines makes the difference between strings and arrays
  69.       //if ( (""+params.constructor).indexOf("function Array()") > -1) {                
  70.       if (params.join) {                            
  71.         parameters = params;                
  72.       } else {
  73.         //probably the method was called in the old fashion way, like this: 
  74.         //NOF.TEXT.MessageFormat.format("{0} bla bla {1}", "BLA", "ALB");                                
  75.         parameters = new Array();
  76.         for (var i = 1; i < arguments.length; i++) {
  77.           parameters[i - 1] = arguments[i];
  78.         }
  79.       }            
  80.       
  81.       var str = pattern;            
  82.       var substr = null;
  83.       var j;
  84.       for (var i=0; i < parameters.length; i++) {
  85.         substr = "{" + i + "}";
  86.         while (true) {
  87.           j = str.indexOf(substr);
  88.           if (j < 0) break;
  89.           str = str.substring(0,j) + parameters[i] + str.substring(j+substr.length, str.length);
  90.         }    
  91.       }
  92.       return str;                            
  93.     }
  94.   }
  95.   // add it to NOF.TEXT namespace
  96.   TEXT.__proto__.MessageFormat = new TEXT_MessageFormat();
  97. }